//节点结构定义
function Node(element) {
    this.data = element;
    this.pre_node = null;
    this.next_node = null;
}

function LinkedQueue() {
    this.length = 0;
    this.front; //队首指针
    this.rear; //队尾指针
}

LinkedQueue.prototype.push = function (element) {
    var node = new Node(element);
    var current;
    node.next_node = null;
    if (this.length == 0) {
        node.pre_node = null;
        node.next_node = null;
        this.front = node;
        this.rear = node;
    } else {
        current = this.rear;
        current.next_node = node;
        node.pre_node = current;
        node.next_node = null;
        this.rear = node;
    }
    this.length++;
    return true;
}

LinkedQueue.prototype.pop = function () {
    if (this.length > 1) {
        var current = this.front;
        this.front = current.next_node;
        this.front.pre_node = null;
        this.length--;
        return current;
    } else if (this.length == 1) {
        var current = this.front;
        this.front.pre_node = null;
        this.front.next_node = null;
        this.length--;
        return current;
    } else {
        return 'Queue is null';
    }
}

LinkedQueue.prototype.size = function () {
    return this.length;
}

LinkedQueue.prototype.getFront = function () {
    return this.front;
}
//获取队尾
LinkedQueue.prototype.getRear = function () {
    return this.rear;
}

LinkedQueue.prototype.toString = function () {
    var str = '',
        current = front;

    while (current) {
        str += current.element;
        current = current.next;
    }

    return str;
}
//清除队列
LinkedQueue.prototype.clear = function () {
    this.front = null;
    this.rear = null;
    this.length = 0;
    return true;
}

function DataParse() {
    this.data_header = [0x55, 0x55]; // 数据校验头数组
    this.header_size = 2; // 数据校验头大小
    this.data_footer = [0xaa, 0xaa]; // 数据校验尾数组
    this.footer_size = 2; // 数据校验尾大小
    this.result_size; // 解析数据大小
    this.queue = new LinkedQueue();

}

DataParse.prototype.parser_put_data = function (data) {
    var node
    this.queue.push(data);

    /* 校验数据尾 */
    node = this.queue.rear;
    for (let i = this.footer_size; i > 0; i--) {
        if (node.data != this.data_footer[i - 1])
            return -1;
        node = node.pre_node;
    }

    /* 校验数据头*/
    var nodeHead = node;
    var findHead = true;
    var n = this.queue.length - this.header_size - this.footer_size + 1;
    var len = 0;
    while (n-- > 0) {
        findHead = true;
        node = nodeHead;
        nodeHead = nodeHead.pre_node;
        for (let i = this.header_size; i > 0; i--) {
            if (node.data != this.data_header[i - 1]) {
                findHead = false;
            }
            node = node.pre_node;
        }

        if (findHead) {
            this.result_size = this.header_size + this.footer_size + len;
            return 0;
        }
        len++;
    }
    this.queue.clear();
    return -1;
}

DataParse.prototype.parser_get_data = function () {
    var len = this.result_size;
    let rbuf = new Uint8Array(len);
    var n = this.queue.size() - len;
    while (n-- > 0) {
        this.queue.pop();
    }
    for (let i = 0; i < len; i++) {
        var tmp = this.queue.pop();
        rbuf[i] = tmp.data;
    }
    return rbuf;
}

module.exports = {
    LinkedQueue: LinkedQueue,
    DataParse: DataParse
}